package org.zj.chat;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class WebSocketNettyServer {
    public static void main(String[] args) {
        NioEventLoopGroup mainGroup = new NioEventLoopGroup(2);
        NioEventLoopGroup subGroup = new NioEventLoopGroup(2);

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();

            serverBootstrap
                    .group(mainGroup, subGroup)
                    .channel(NioServerSocketChannel.class) //指定通道类型
                    .childHandler(new WebSocketChannelInitializer());
            System.out.println("服务器启动");
            //绑定端口启动服务器
            ChannelFuture future = serverBootstrap.bind(8082).sync();

            //等待服务器关闭

            future.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            //关闭服务区
            mainGroup.shutdownGracefully();
            subGroup.shutdownGracefully();
        }


    }
}
